package com.ymatou.iapi.delivery.testcase;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.delivery.parameter.DeliveryInfo;
import com.ymatou.iapi.delivery.parameter.ModfiyDeliveryCompanyReq;
import com.ymatou.iapi.delivery.service.DeliveryService;
import com.ymatou.iapi.delivery.service.ModifyDeliveryCompanyCall;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.sharding.db.ext.model.DeliveryPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.database.sqlwapper.DefaultWapper;
/**
*修改物流公司接口(接口废弃)
*FreeMaker Template自动生成代码
*/
@TestSuite
@Ignore
public class Ts_modifyDeliveryCompany {

	private static ModfiyDeliveryCompanyReq req;
	private static ModifyDeliveryCompanyCall call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("校验物流单号接口");
	}

	@Before
	public void caseUp() {
		req = new ModfiyDeliveryCompanyReq();
		call = new ModifyDeliveryCompanyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 校验 mysql delivery 表
	 * @param info
	 */
	private void verifyMysqlDb(DeliveryInfo info, long sellerId,
			int logisticsType) {

		long orderId = info.getOrderId();

		DeliveryPo actualDeliveryPo = new OrderWapper()
				.selectDeliveryByOrderId(orderId);
		int deliveryPoSize =  new OrderWapper().selectDeliverySizeByOrderId(orderId);

		Logger.comment("校验 delivery 表数据");

		Logger.verifyEquals(info.getYmtExpressCode(),
				actualDeliveryPo.getYmtExpressCode(), "验证数据库YmtExpressCode");

		Logger.verifyEquals(false, actualDeliveryPo.getOfficalLogistics(),
				"验证数据库OfficalLogistics");

		Logger.verifyEquals(Utils.zeroIfNull(info.getRealDeliveryArea()),
				actualDeliveryPo.getRealDeliveryArea(), "验证数据库RealDeliveryArea");

		/*		Logger.verifyEquals(actualDeliveryPo.getUpdateTime(),
						actual.getUpdateTime(), "验证数据库UpdateTime");
				Logger.verifyEquals(actualDeliveryPo.getCreateTime(),
						actual.getCreateTime(), "验证数据库CreateTime");*/

		Logger.verifyEquals(orderId, actualDeliveryPo.getOrderId(),
				"验证数据库OrderId");

		Logger.verifyEquals(sellerId, actualDeliveryPo.getSellerId(),
				"验证数据库SellerId");

		Logger.verifyEquals(logisticsType, actualDeliveryPo.getLogisticsType(),
				"验证数据库LogisticsType");

		Logger.verifyEquals(false, actualDeliveryPo.getDeleted(),
				"验证数据库Deleted");

		Logger.verifyEquals(info.getDeliverySource(),
				actualDeliveryPo.getSource(), "验证数据库Source");
		
		if (logisticsType == LogisticsTypeEnum.OVERSEAS_DELIVERY.getCode()
                && info.getDeliverySection() == 3
                && deliveryPoSize == 1)
			Logger.verifyEquals(true, actualDeliveryPo.getSourceTrack(),
					"验证数据库SourceTrack");			
		else
			Logger.verifyEquals(false, actualDeliveryPo.getSourceTrack(),
					"验证数据库SourceTrack");

		Logger.verifyEquals(info.getBillNo(), actualDeliveryPo.getBillNo(),
				"验证数据库BillNo");

		Logger.verifyEquals(info.getDeliverySection(),
				actualDeliveryPo.getSection(), "验证数据库Section");

		if (DeliveryService.isWriteSqlServer) {

			verifySqlServerDb(info, sellerId, logisticsType);
		}

	}
	/**
	 * 校验 sql Ymt_BillCodeSummary Ymt_BillCodeSummaryToOrders 表
	 * @param info
	 */
	@SuppressWarnings("rawtypes")
	private void verifySqlServerDb(DeliveryInfo info, Long sellerId,
			int logisticsType) {

		Long orderId = info.getOrderId();

		String sqlString = String
				.format("SELECT * FROM dbo.Ymt_BillCodeSummary x INNER JOIN dbo.Ymt_BillCodeSummaryToOrders "
						+ "y ON x.billCodeSummaryID = y.BillCodeSummaryID where orderid=%s",
						orderId);

		Logger.comment("校验 Ymt_BillCodeSummary , Ymt_BillCodeSummaryToOrders");

		List<Map> res = new DefaultWapper().executeSql(sqlString);

		if (CollectionUtils.isEmpty(res)) {

			Logger.comment("sql server Ymt_BillCodeSummary 数据为空 ");

			Logger.fail(new Exception("sql server Ymt_BillCodeSummary 数据为空"));

		}

		Map actualBillCodeSummary = res.get(0);

		Logger.verifyEquals(sellerId.intValue(),
				(Integer) actualBillCodeSummary.get("SellerID"),
				"验证数据库SellerID");

		Logger.verifyEquals(info.getBillNo(),
				(String) actualBillCodeSummary.get("billCode"), "验证数据库billCode");

		/*		Logger.verifyEquals(logisticsType,
						(Integer) actualBillCodeSummary.get("DeliverType"),
						"验证数据库DeliverType");*/

		Logger.verifyEquals(info.getYmtExpressCode(),
				(String) actualBillCodeSummary.get("InnerExpressCode"),
				"验证数据库InnerExpressCode");

		Logger.verifyEquals(false,
				(Boolean) actualBillCodeSummary.get("IsOfficalLogistic"),
				"验证数据库IsOfficalLogistic");

		Logger.verifyEquals(
				info.getDeliverySection() == 3
						|| info.getDeliverySection() == 2,
				(Boolean) actualBillCodeSummary.get("IsChinaDeliver"),
				"验证数据库IsChinaDeliver");

		Logger.verifyEquals(info.getDeliverySection() == 2 ? 1 : null,
				(Integer) actualBillCodeSummary.get("Tranship"),
				"验证数据库Tranship");

		Logger.verifyEquals(false,
				(Boolean) actualBillCodeSummary.get("IsDeleted"),
				"验证数据库IsDeleted");

		Logger.verifyEquals(logisticsType,
				(Integer) actualBillCodeSummary.get("CatalogStatus"),
				"验证数据库CatalogStatus");

		Logger.verifyEquals(Utils.zeroIfNull(info.getRealDeliveryArea()) == 1,
				actualBillCodeSummary.get("ShipByHK"), "验证数据库ShipByHK");

		Logger.verifyEquals(orderId.intValue(),
				(Integer) actualBillCodeSummary.get("OrderID"), "验证数据库Orde1rID");

	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyDeliveryCompany_001() {
		Logger.start(true, "普通拼邮 国际段  发货之后,修改物流公司成功");
		try {

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			String billNo = TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			req.setOrderId(orderId);
			req.setBillNo(billNo);
			req.setOldYmtExpressCode("Y013");
			req.setYmtExpressCode(DeliveryService.YmtExpressCode4);
			req.setSellerId(sellerId);

			call.setData(req);
			call.callService();
			
			DeliveryInfo deliverInfo = new DeliveryInfo();
			deliverInfo.setOrderId(orderId);
			deliverInfo.setBillNo(req.getBillNo());
			deliverInfo.setYmtExpressCode(req.getYmtExpressCode());
			deliverInfo.setDeliverySource(1);
			deliverInfo.setDeliverySection(1);

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "true",
					"校验IsSuccess");

			verifyMysqlDb(deliverInfo, sellerId, 7);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyDeliveryCompany_003() {
		Logger.start(true, "普通拼邮 国内段  发货之后,修改物流公司成功");
		try {

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			String billNo = TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(10000);

			req.setOrderId(orderId);
			req.setBillNo(billNo);
			req.setOldYmtExpressCode(DeliveryService.YmtExpressCode1);
			req.setYmtExpressCode(DeliveryService.YmtExpressCode5);
			req.setSellerId(sellerId);

			call.setData(req);
			call.callService();

			DeliveryInfo deliverInfo = new DeliveryInfo();
			deliverInfo.setOrderId(orderId);
			deliverInfo.setBillNo(billNo);
			deliverInfo.setYmtExpressCode(req.getYmtExpressCode());
			deliverInfo.setDeliverySource(1);
			deliverInfo.setDeliverySection(3);

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "true",
					"校验IsSuccess");

			verifyMysqlDb(deliverInfo, sellerId, 7);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}